package xiao_jin.fantastic_journey.core.init;

import club.someoneice.cookie.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.mojang.datafixers.util.Pair;
import net.minecraft.block.*;
import net.minecraft.item.BlockItem;
import net.minecraft.item.HoeItem;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import xiao_jin.fantastic_journey.FantasticJourney;
import xiao_jin.fantastic_journey.core.common.block.CultivatedWasteSoil;
import xiao_jin.fantastic_journey.core.common.block.SilicifiedWasteSoil;
import xiao_jin.fantastic_journey.core.common.block.relictechentity.RelictechBaseStoneBlock;
import xiao_jin.fantastic_journey.core.common.block.relictechentity.RelictechQuartetLockBlock;
import xiao_jin.fantastic_journey.core.common.block.relictechentity.RelictechTableBlock;
import xiao_jin.fantastic_journey.util.ModUtil;


public final class ModBlocks {
    public static final Block EXPLOSIVE_STEEL_BLOCK = registerBlock("explosive_steel_block", new Block(AbstractBlock.Settings.copy(Blocks.IRON_BLOCK).mapColor(MapColor.RAW_IRON_PINK)), ModItemGroup.Blocks);

    /* 普通方块 */
    public static final Block WASTE_SOIL = registerBlock("waste_soil", new Block(AbstractBlock.Settings.copy(Blocks.DIRT).strength(0.7f)), ModItemGroup.Blocks);
    public static final Block SILICIFIED_WASTE_SOIL = registerBlock("silicified_waste_soil", new SilicifiedWasteSoil(), ModItemGroup.Blocks);
    public static final Block CULTIVATED_WASTE_SOIL = registerBlock("cultivated_waste_soil", new CultivatedWasteSoil(), ModItemGroup.Blocks);

    /* 遗械石及变体方块 */
    public static final Block RELICTECH_STONE = registerBlock("relictech_stone", new Block(AbstractBlock.Settings.copy(Blocks.STONE)), ModItemGroup.Blocks);
    public static final Block RELICTECH_STONE_STAIRS = registerBlock("relictech_stone_stairs", new StairsBlock(ModBlocks.RELICTECH_STONE.getDefaultState(), AbstractBlock.Settings.copy(Blocks.COBBLESTONE_STAIRS)), ModItemGroup.Blocks);
    public static final Block RELICTECH_STONE_SLAB = registerBlock("relictech_stone_slab", new SlabBlock(AbstractBlock.Settings.copy(Blocks.COBBLESTONE_SLAB)), ModItemGroup.Blocks);
    public static final Block RELICTECH_STONE_WALL = registerBlock("relictech_stone_wall", new WallBlock(AbstractBlock.Settings.copy(Blocks.COBBLESTONE_WALL)), ModItemGroup.Blocks);
    public static final Block RELICTECH_STONE_BRICKS = registerBlock("relictech_stone_bricks", new Block(AbstractBlock.Settings.copy(Blocks.STONE).strength(2.0f, 8.0f)), ModItemGroup.Blocks);
    public static final Block RELICTECH_STONE_BRICKS_STAIRS = registerBlock("relictech_stone_bricks_stairs", new StairsBlock(ModBlocks.RELICTECH_STONE_BRICKS.getDefaultState(), AbstractBlock.Settings.copy(Blocks.COBBLESTONE_STAIRS).strength(2.0f, 8.0f)), ModItemGroup.Blocks);
    public static final Block RELICTECH_STONE_BRICKS_SLAB = registerBlock("relictech_stone_bricks_slab", new SlabBlock(AbstractBlock.Settings.copy(Blocks.COBBLESTONE_SLAB).strength(2.0f, 8.0f)), ModItemGroup.Blocks);
    public static final Block RELICTECH_STONE_BRICKS_WALL = registerBlock("relictech_stone_bricks_wall", new WallBlock(AbstractBlock.Settings.copy(Blocks.COBBLESTONE_WALL).strength(2.0f, 8.0f)), ModItemGroup.Blocks);
    public static final Block RELICTECH_CRACK_STONE = registerBlock("relictech_crack_stone", new Block(AbstractBlock.Settings.copy(Blocks.STONE)), ModItemGroup.Blocks);
    public static final Block RELICTECH_CRACK_STONE_STAIRS = registerBlock("relictech_crack_stone_stairs", new StairsBlock(ModBlocks.RELICTECH_STONE.getDefaultState(), AbstractBlock.Settings.copy(Blocks.COBBLESTONE_STAIRS)), ModItemGroup.Blocks);
    public static final Block RELICTECH_CRACK_STONE_SLAB = registerBlock("relictech_crack_stone_slab", new SlabBlock(AbstractBlock.Settings.copy(Blocks.COBBLESTONE_SLAB)), ModItemGroup.Blocks);
    public static final Block RELICTECH_CRACK_STONE_WALL = registerBlock("relictech_crack_stone_wall", new WallBlock(AbstractBlock.Settings.copy(Blocks.COBBLESTONE_WALL)), ModItemGroup.Blocks);
    public static final Block RELICTECH_CRACK_STONE_BRICKS = registerBlock("relictech_crack_stone_bricks", new Block(AbstractBlock.Settings.copy(Blocks.STONE).strength(2.0f, 8.0f)), ModItemGroup.Blocks);
    public static final Block RELICTECH_CRACK_STONE_BRICKS_STAIRS = registerBlock("relictech_crack_stone_bricks_stairs", new StairsBlock(ModBlocks.RELICTECH_CRACK_STONE_BRICKS.getDefaultState(), AbstractBlock.Settings.copy(Blocks.COBBLESTONE_STAIRS).strength(2.0f, 8.0f)), ModItemGroup.Blocks);
    public static final Block RELICTECH_CRACK_STONE_BRICKS_SLAB = registerBlock("relictech_crack_stone_bricks_slab", new SlabBlock(AbstractBlock.Settings.copy(Blocks.COBBLESTONE_SLAB).strength(2.0f, 8.0f)), ModItemGroup.Blocks);
    public static final Block RELICTECH_CRACK_STONE_BRICKS_WALL = registerBlock("relictech_crack_stone_bricks_wall", new WallBlock(AbstractBlock.Settings.copy(Blocks.COBBLESTONE_WALL).strength(2.0f, 8.0f)), ModItemGroup.Blocks);

    /* 深层遗械石及变体方块 */
    public static final Block DEEP_RELICTECH_STONE = registerBlock("deep_relictech_stone", new Block(AbstractBlock.Settings.copy(Blocks.DEEPSLATE)), ModItemGroup.Blocks);
    public static final Block DEEP_RELICTECH_STONE_STAIRS = registerBlock("deep_relictech_stone_stairs", new StairsBlock(ModBlocks.DEEP_RELICTECH_STONE.getDefaultState(), AbstractBlock.Settings.copy(Blocks.COBBLESTONE_STAIRS)), ModItemGroup.Blocks);
    public static final Block DEEP_RELICTECH_STONE_SLAB = registerBlock("deep_relictech_stone_slab", new SlabBlock(AbstractBlock.Settings.copy(Blocks.DEEPSLATE_BRICK_SLAB)), ModItemGroup.Blocks);
    public static final Block DEEP_RELICTECH_STONE_WALL = registerBlock("deep_relictech_stone_wall", new WallBlock(AbstractBlock.Settings.copy(Blocks.DEEPSLATE_BRICK_WALL)), ModItemGroup.Blocks);

    /* 特殊遗械方块 */
    public static final Block RELICTECH_BASE_STONE = registerBlock("relictech_base_stone", new RelictechBaseStoneBlock(AbstractBlock.Settings.copy(Blocks.STONE).strength(2.0f, 8.0f)), ModItemGroup.FunctionalBlock);
    public static final Block RELICTECH_QUARTET_LOCK = registerBlock("relictech_quartet_lock", new RelictechQuartetLockBlock(AbstractBlock.Settings.copy(Blocks.STONE).strength(2.0f, 8.0f)), ModItemGroup.FunctionalBlock);
    public static final Block RELICTECH_TABLE = registerBlock("relictech_table", new RelictechTableBlock(AbstractBlock.Settings.copy(Blocks.STONE).strength(2.0f, 8.0f).luminance(state -> state.get(RelictechTableBlock.ACTIVATION) ? 3 : 0)), ModItemGroup.FunctionalBlock);

    /* 遗械矿石 */
    public static final Block RELICTECH_COPPER_ORE = registerBlock("relictech_copper_ore", new Block(AbstractBlock.Settings.copy(Blocks.COPPER_ORE)), ModItemGroup.Blocks);
    public static final Block DEEP_RELICTECH_COPPER_ORE = registerBlock("deep_relictech_copper_ore", new Block(AbstractBlock.Settings.copy(Blocks.DEEPSLATE_COPPER_ORE)), ModItemGroup.Blocks);
    public static final Block RELICTECH_EMERALD_ORE = registerBlock("relictech_emerald_ore", new Block(AbstractBlock.Settings.copy(Blocks.EMERALD_ORE)), ModItemGroup.Blocks);
    public static final Block DEEP_RELICTECH_EMERALD_ORE = registerBlock("deep_relictech_emerald_ore", new Block(AbstractBlock.Settings.copy(Blocks.DEEPSLATE_EMERALD_ORE)), ModItemGroup.Blocks);
    public static final Block RELICTECH_GOLD_ORE = registerBlock("relictech_gold_ore", new Block(AbstractBlock.Settings.copy(Blocks.GOLD_ORE)), ModItemGroup.Blocks);
    public static final Block DEEP_RELICTECH_GOLD_ORE = registerBlock("deep_relictech_gold_ore", new Block(AbstractBlock.Settings.copy(Blocks.DEEPSLATE_GOLD_ORE)), ModItemGroup.Blocks);
    public static final Block RELICTECH_IRON_ORE = registerBlock("relictech_iron_ore", new Block(AbstractBlock.Settings.copy(Blocks.IRON_ORE)), ModItemGroup.Blocks);
    public static final Block DEEP_RELICTECH_IRON_ORE = registerBlock("deep_relictech_iron_ore", new Block(AbstractBlock.Settings.copy(Blocks.DEEPSLATE_IRON_ORE)), ModItemGroup.Blocks);
    public static final Block RELICTECH_LAPIS_ORE = registerBlock("relictech_lapis_ore", new Block(AbstractBlock.Settings.copy(Blocks.LAPIS_ORE)), ModItemGroup.Blocks);
    public static final Block DEEP_RELICTECH_LAPIS_ORE = registerBlock("deep_relictech_lapis_ore", new Block(AbstractBlock.Settings.copy(Blocks.DEEPSLATE_LAPIS_ORE)), ModItemGroup.Blocks);
    public static final Block RELICTECH_REDSTONE_ORE = registerBlock("relictech_redstone_ore", new RedstoneOreBlock(AbstractBlock.Settings.copy(Blocks.REDSTONE_ORE)), ModItemGroup.Blocks);
    public static final Block DEEP_RELICTECH_REDSTONE_ORE = registerBlock("deep_relictech_redstone_ore", new RedstoneOreBlock(AbstractBlock.Settings.copy(Blocks.DEEPSLATE_REDSTONE_ORE)), ModItemGroup.Blocks);
    public static final Block RELICTECH_DIAMOND_ORE = registerBlock("relictech_diamond_ore", new Block(AbstractBlock.Settings.copy(Blocks.DIAMOND_ORE)), ModItemGroup.Blocks);
    public static final Block DEEP_RELICTECH_DIAMOND_ORE = registerBlock("deep_relictech_diamond_ore", new Block(AbstractBlock.Settings.copy(Blocks.DEEPSLATE_DIAMOND_ORE)), ModItemGroup.Blocks);

    private static Block registerBlock(String name, Block block, ModItemGroup itemGroup) {
        block = Registry.register(Registries.BLOCK, ModUtil.getIdOfMod(name), block);
        var item = Registry.register(Registries.ITEM, ModUtil.getIdOfMod(name), new BlockItem(block, new Item.Settings()));
        var map = FantasticJourney.ITEM_GROUP_MAP;
        map.put(itemGroup, ObjectUtil.objectRun(map.getOrDefault(itemGroup, Lists.newArrayList()), it -> {
            it.add(item);
        }));
        return block;
    }

    public static void registerModBlocks() {
        HoeItem.TILLING_ACTIONS.put(ModBlocks.WASTE_SOIL, Pair.of(HoeItem::canTillFarmland, HoeItem.createTillAction(ModBlocks.CULTIVATED_WASTE_SOIL.getDefaultState())));
        HoeItem.TILLING_ACTIONS.put(ModBlocks.SILICIFIED_WASTE_SOIL, Pair.of(HoeItem::canTillFarmland, HoeItem.createTillAction(ModBlocks.CULTIVATED_WASTE_SOIL.getDefaultState())));
    }


}
